/*
 * 系统初始化入口
 */

//模块基类
Ext.namespace('Application');   
Application.module = function(tab){
    this.main = tab;
    this.init();
}   
//定义模块基类
Ext.extend(Application.module, Ext.util.Observable, {   
    init : Ext.emptyFn
});

//主程序类 
Application.main = function(){    
    this.init();   
}  
Ext.extend(Application.main, Ext.util.Observable, {   
    init: function(){
    	this.tree = new Ext.ux.tree.CompUrlTree({
			dataUrl:WEB_CONTEXT + '/system/menuTree',
			rootText:'系统管理',
			region:'west',
			contentEl:'west'
		});
		this.tree.getRootNode().expand();
		this.tree.on('click',this.clickTree, this);      
        
        var tab =  new Ext.Panel({
            title: '说明',   
            id: 'docs',
            bodyStyle:'padding:10px',
            autoLoad: 'docs.html', 
            layout: 'fit'  
        }); 
        this.tabPanel = new Ext.TabPanel({   
        	id:'tabs',
            region:'center',   
            margins:'0 5 0 0',
            activeTab: 0,
            autoScroll: true,   
            items: [tab]   
        }); 
        this.tabPanel.on('tabchange', this.changeTab, this); 
  
        var viewport = new Ext.Viewport({   
            layout:'border',   
            items:[   
                new Ext.BoxComponent({region:'north', contentEl:'north', height:60}),   
                new Ext.BoxComponent({region:'south', contentEl:'south', height:50}),
                this.tree,  
                this.tabPanel   
            ]   
        });
        this.loadMask = new Ext.LoadMask(this.tabPanel.body);          
    },
    
    //切换TabPanel标签方法
    changeTab: function(p,t){
        //如果存在相应树节点，就选中,否则就清空选择状态
        var id = t.id.replace('tab-','');
        var node = this.tree.getNodeById(id);
        if(node){
            this.tree.getSelectionModel().select(node);
        }else{
            this.tree.getSelectionModel().clearSelections();
        }      
    },
    
    //点击菜单方法(node:被点击的节点)
    clickTree: function(node){
        //如果节点不是叶子则返回
        if(!node.isLeaf()){   
            return false;   
        }
        var id = 'tab-' + node.id;
        var tab = Ext.getCmp(id);
        if(!tab){
        	if("IFRAME" == node.attributes.type) {
            	tab = this.tabPanel.add(new Ext.ux.ManagedIFrame.Panel({
						id : node.id,
						title : node.text,
						closable : true,
						layout : 'fit',
						defaultSrc : WEB_CONTEXT + node.attributes.url,
						loadMask : true
					}));
            	this.tabPanel.setActiveTab(tab);
            } else {
	            tab = this.tabPanel.add(new Ext.Panel({
	                id: id,
	                title: node.text,
	                layout: 'fit',
	                closable: true
	            }));
	            //加载模块
            	this.tabPanel.setActiveTab(tab);
	            this.loadModule(node,tab);
            }
        }else{
            this.tabPanel.setActiveTab(tab);
        }
    },
    
    //加载模块方法(id:模块ID;tab:模块显示Panel)
    loadModule: function(node,tab){
        //定义模块变量
        var module;
        if(this[node.id]){
            //如果模块类已存在，就直接实例化
            module = new this[node.id](tab);
        }else{
            this.loadMask.show();
            Ext.Ajax.request({   
                method:'GET',
                //url: 'modules/'+id+'.js',   
                url:WEB_CONTEXT + node.attributes.url,
                scope: this,
                success: function(response){
                    //获取模块类  
                    this[node.id] = eval(response.responseText);
                    //实例化模块类
                    module = new this[node.id](tab);
                    this.loadMask.hide();
                }   
            });
        }
    }
});

Ext.onReady(function(){   
    Ext.QuickTips.init();   
    sysApp = new Application.main();   
});